import 'package:flutter/material.dart';

class Person {
  String name;

  int age;

  Person(this.name, this.age);

  @override
  String toString() {
    return "我叫$name,今年$age了";
  }
}

class InheriedDataWidget extends InheritedWidget {
  final Person person; //需要在树中共享的数据

  const InheriedDataWidget({super.key, required this.person, required Widget child})
      : super(child: child);

  //定义一个便捷方法，方便子树中的widget获取共享数据
  static InheriedDataWidget? of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<InheriedDataWidget>();
  }

  @override
  bool updateShouldNotify(InheriedDataWidget oldWidget) {
    // TODO: implement updateShouldNotify
    //如果返回true，则子树中依赖(build函数中有调用)本widget的子 .
    //widget的`state.didChangeDependencies`方法会被调用
    return person.name != oldWidget.person.name ||
        person.age != oldWidget.person.age;
  }
}

//WidgetA
class WidgetA extends StatefulWidget {
  const WidgetA({super.key});

  @override
  _WidgetAState createState() => _WidgetAState();
}

class _WidgetAState extends State<WidgetA> {
  Person person = Person("小明", 24);

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    //当WidgetA中Person数据发生改变时，此方法会被调用，
    //前提是updateShouldNotify返回true
    print("父widget_ful===>didChangeDependencies");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        //调用构造，在此节点共享了一个Person类型的数据
        child: InheriedDataWidget(
          person: person,
          child: const WidgetA1(),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            //在setState中刷新Person的值
            person = Person(person.name, person.age + 1);
          });
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}

//控件WidgetA
class WidgetA1 extends StatelessWidget {
  const WidgetA1({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.grey,
      width: MediaQuery.of(context).size.width,
      margin: const EdgeInsets.all(10),
      child: Column(
        children: <Widget>[
          const Text(
            "A1 Widget",
            style: TextStyle(color: Colors.white, fontSize: 20),
          ),
          //调用InheriedDataWidget.of()获取数据，其实也就是调用了 .
          //inheritFromWidgetOfExactType方法
          Text(
            "共享数据的信息: ${InheriedDataWidget.of(context)?.person.toString()}",
            style: const TextStyle(color: Colors.white, fontSize: 20),
          ),
          Container(
            margin: const EdgeInsets.only(top: 40),
            color: Colors.deepOrangeAccent,
            height: MediaQuery.of(context).size.height * 0.5,
            width: MediaQuery.of(context).size.width,
            child: const Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                WidgetA1_1(),
                WidgetA1_2(),
                WidgetA1_3(),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

class WidgetA1_1 extends StatefulWidget {
  const WidgetA1_1({super.key});

  @override
  _WidgetA1_1State createState() => _WidgetA1_1State();
}

class _WidgetA1_1State extends State<WidgetA1_1> {
  @override
  Widget build(BuildContext context) {
    print("WidgetA1_1_ful ===>build");
    return Container(
      color: Colors.white,
      height: 140,
      width: MediaQuery.of(context).size.width / 3 - 20,
      //引用共享数据
      child: Text(
          "WidgetA1_1ful\n\n共享数据是：${InheriedDataWidget.of(context)?.person.toString()}"),
    );
  }

  @override
  void didUpdateWidget(covariant WidgetA1_1 oldWidget) {
    super.didUpdateWidget(oldWidget);
    print("WidgetA1_1ful===>didUpdateWidget");
  }

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    //当WidgetA中Person数据发生改变时，此方法会被调用，
    //前提是updateShouldNotify返回true
    print("WidgetA1_1ful===>didChangeDependencies");
  }
}

class WidgetA1_2 extends StatelessWidget {
  const WidgetA1_2({super.key});

  @override
  Widget build(BuildContext context) {
    print("WidgetA1_2===>build");
    return Container(
      color: Colors.white,
      height: 140,
      width: MediaQuery.of(context).size.width / 3 - 20,
      child: Text(
          "WidgetA1_2less\n\n共享数据是：${InheriedDataWidget.of(context)?.person.toString()}"),
    );
  }
}

class WidgetA1_3 extends StatefulWidget {
  const WidgetA1_3({super.key});

  @override
  _WidgetA1_3State createState() => _WidgetA1_3State();
}

class _WidgetA1_3State extends State<WidgetA1_3> {
  @override
  Widget build(BuildContext context) {
    print("WidgetA1_3_ful===>build");
    return Container(
      color: Colors.white,
      height: 140,
      width: MediaQuery.of(context).size.width / 3 - 20,
      child: const Text("WidgetA1_3ful\n\n未引用共享数据信息"),
    );
  }

  @override
  void didUpdateWidget(covariant WidgetA1_3 oldWidget) {
    super.didUpdateWidget(oldWidget);
    print("WidgetA1_3_ful===>didUpdateWidget");
  }

  @override
  void didChangeDependencies() {
    // TODO: implement didChangeDependencies
    super.didChangeDependencies();
    print("WidgetA1_3_ful===>didChangeDependencies");
  }
}
